package socket.nio.demo_03;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

public class BlockingClent {
    private static final int DEFAULT_PORT = 8080;
    private Socket socket;

    public static void main(String[] args) {
        new BlockingClent().word(DEFAULT_PORT);
    }

    public void word(int port) {
        try {
            socket = new Socket();
            socket.connect(new InetSocketAddress("localhost", DEFAULT_PORT));

            InputStreamReader in = new InputStreamReader(socket.getInputStream());
            OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());

            out.write("你好");
            out.flush();
            out.close();

            StringBuilder buffer = new StringBuilder(100);
            int count =0;
            for (int c = in.read(); c != -1; c = in.read()) {
                count++;
                buffer.append((char) c);
                if(count == 100) {
                    count =0;
                    System.out.println(buffer.toString());
                    buffer = new StringBuilder();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
